package util;

import static java.util.Calendar.MONTH;
import static java.util.Calendar.YEAR;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateUtils extends org.apache.commons.lang.time.DateUtils {
    
    public static String YYYY_MM_DD_HH_MM_SSLLL = "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL";
    public static String YYYY_MM_DD_HH_MM_SS = "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS";
    public static String YYYY_MM_DD = "%1$tY-%1$tm-%1$td";
    public static String YYYY_MM = "%1$tY-%1$tm";
    public static String MM_DD = "%1$tm-%1$td";

    public static String STRANDARD_DATE1 = "%1$tY年%1$tm月%1$td日";
    public static String STRANDARD_DATE2 = "%1$tY年%1$tm月";
    
    public static Map<Integer, String> CHINESE_YEAR_NUM = new HashMap<Integer, String>(0);
    public static Map<Integer, String> CHINESE_DAY = new HashMap<Integer, String>(0);
    public static Map<Integer, String> CHINESE_WEEK = new HashMap<Integer, String>(0);
    static {
        CHINESE_YEAR_NUM.put(0, "○");
        CHINESE_YEAR_NUM.put(1, "一");
        CHINESE_YEAR_NUM.put(2, "二");
        CHINESE_YEAR_NUM.put(3, "三");
        CHINESE_YEAR_NUM.put(4, "四");
        CHINESE_YEAR_NUM.put(5, "五");
        CHINESE_YEAR_NUM.put(6, "六");
        CHINESE_YEAR_NUM.put(7, "七");
        CHINESE_YEAR_NUM.put(8, "八");
        CHINESE_YEAR_NUM.put(9, "九");

        CHINESE_WEEK.put(1, "星期日");
        CHINESE_WEEK.put(2, "星期一");
        CHINESE_WEEK.put(3, "星期二");
        CHINESE_WEEK.put(4, "星期三");
        CHINESE_WEEK.put(5, "星期四");
        CHINESE_WEEK.put(6, "星期五");
        CHINESE_WEEK.put(7, "星期六");

        CHINESE_DAY.put(1, "一");
        CHINESE_DAY.put(2, "二");
        CHINESE_DAY.put(3, "三");
        CHINESE_DAY.put(4, "四");
        CHINESE_DAY.put(5, "五");
        CHINESE_DAY.put(6, "六");
        CHINESE_DAY.put(7, "七");
        CHINESE_DAY.put(8, "八");
        CHINESE_DAY.put(9, "九");
        CHINESE_DAY.put(10, "十");
        CHINESE_DAY.put(11, "十一");
        CHINESE_DAY.put(12, "十二");
        CHINESE_DAY.put(13, "十三");
        CHINESE_DAY.put(14, "十四");
        CHINESE_DAY.put(15, "十五");
        CHINESE_DAY.put(16, "十六");
        CHINESE_DAY.put(17, "十七");
        CHINESE_DAY.put(18, "十八");
        CHINESE_DAY.put(19, "十九");
        CHINESE_DAY.put(20, "二十");
        CHINESE_DAY.put(21, "二十一");
        CHINESE_DAY.put(22, "二十二");
        CHINESE_DAY.put(23, "二十三");
        CHINESE_DAY.put(24, "二十四");
        CHINESE_DAY.put(25, "二十五");
        CHINESE_DAY.put(26, "二十六");
        CHINESE_DAY.put(27, "二十七");
        CHINESE_DAY.put(28, "二十八");
        CHINESE_DAY.put(29, "二十九");
        CHINESE_DAY.put(30, "三十");
        CHINESE_DAY.put(31, "三十一");
    }

    public DateUtils() {
    }

    public static int difftime(long long1, long long2, int field) {
        return difftime(new Date(long1), new Date(long2), field);
    }
    
    public static int difftime(Date date1, Date date2, int field) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(date1);
        c2.setTime(date2);
        return difftime(c1, c2, field);
    }
    
    public static int difftime(Calendar c1, Calendar c2, int field) {
        
        int margin = 0;
        switch (field) {
            case Calendar.YEAR:
                margin = c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
                break;
                
            case Calendar.MONTH:
                margin = (c1.get(YEAR) * 12 + c1.get(MONTH))
                        - (c2.get(YEAR) * 12 + c2.get(MONTH));
                break;
                
            case Calendar.DAY_OF_MONTH:
                margin = (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / 60 / 60 / 24 / 1000);
                break;
                
            case Calendar.HOUR_OF_DAY:
                margin = (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / 60 / 60 / 24 / 1000);
                break;
                
            case Calendar.MINUTE:
                margin = (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / 60 / 60 / 24 / 1000);
                break;
                
            case Calendar.SECOND:
                margin = (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / 60 / 60 / 24 / 1000);
                break;
                
            case Calendar.MILLISECOND:
                margin = (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / 60 / 60 / 24 / 1000);
                break;
                
            default:
                throw new IllegalArgumentException();
        }
        return margin;
    }
}
